﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace Public
{
    public class DataProtector
    {
        /// <summary>
        /// MD5算法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string MD5Stirng(string value)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(value);
            byte[] result = md5.ComputeHash(data);
            string ret = "";
            for (int i = 0; i < result.Length; i++)
                ret += result[i].ToString("x2");
            return ret;
        }



        /// <summary>
        /// DataProtector 的摘要说明。
        /// </summary>
        #region 构造函数
        public DataProtector() { }
        #endregion

        #region 自定义变量

        //简单加密
        public static string SECRET = "kjhqjherqiudnemnxsaskjjkhqewurhbdnbaqjdquermqeksmeerkqelkjdqejhefjqfeqwfrqdjmqjejfndewffheqwkhbgeqwdeqwdxeqwdhbeqwdjqwdkjhdemgfhdjeqwhfbjwfkbeqwhbdvhgftwqweurbdqliyhkjhqjherqizxcvmnxsaskjjkhqewurhbdnbaqjdquermqerjqnerkqelkjujmikefjqfeqwfrqdjmqjejfnwerfvheqwkhbgeqwdeqwdxeqwdhbeqwdjqwdkjheqwjfhdjeqwhfbjwfkbeqwhbdvhgftwgdiurbdqliyhkjhqjherqiheramnxsaskjjkhqewurhbdnbaqjdquermqerjqnerkqelkjdqejhefjqfeqwfrqdjmqjejfndewffheqwkhbgeqwdeqwdxeqwdhbeqwdjqwdkjheqwjfhdjeqwhfbjwfkbeqwhbdvhgftwgdiurbdqlidd";

        //密钥
        private const string keyString = "cadbN1bbYXw=";
        //向量
        private const string ivString = "uu+VET39b6M=";
        #endregion

        #region 加密和解密字符串
        /// <summary>
        /// 加密字符串
        /// </summary>
        /// <param name="Value">加密前的字符串</param>
        /// <returns>加密后返回的字符串</returns>
        public static string EncryptString(string Value)
        {

            SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();

            mCSP.Key = Convert.FromBase64String(keyString);
            mCSP.IV = Convert.FromBase64String(ivString);

            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;

            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);

            byt = Encoding.UTF8.GetBytes(Value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return Convert.ToBase64String(ms.ToArray());

        }

        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="Value">解密前的字符串</param>
        /// <returns>返回解密后的字符串</returns>
        public static string DecryptString(string Value)
        {
            try
            {
                SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
                mCSP.Key = Convert.FromBase64String(keyString);
                mCSP.IV = Convert.FromBase64String(ivString);

                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;

                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);

                byt = Convert.FromBase64String(Value);

                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();

                cs.Close();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch
            {
                return "-100";
            }

        }
        #endregion

        #region  简单加密

        /// <summary>
        ///  加密
        /// </summary>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string SimpleEncode(string strSrc)
        {
            return SimpleEncode(strSrc, SECRET);

        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="strSrc"></param>
        /// <param name="constSecret"></param>
        /// <returns></returns>
        public static string SimpleEncode(string strSrc, string constSecret)
        {
            string str = "";
            try
            {
                if (!strSrc.Equals(""))
                {
                    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                    char[] cnB = constSecret.ToCharArray(0, constSecret.Length);
                    byte[] show = encoding.GetBytes(strSrc);
                    for (int i = 0; i < show.Length; i++)
                    {
                        int kn = i % (constSecret.Length);
                        int sum = (int)cnB[kn] + (int)show[i];
                        string s = "";
                        if (sum < 100 && sum >= 10)
                            s = "0" + sum.ToString();
                        else if (sum < 10)
                            s = "00" + sum.ToString();
                        else s = sum.ToString();
                        str = str + s;
                    }
                }
            }
            catch //(Exception x)
            {
                //Exception y = new Exception(x.Message + "\n" + x.StackTrace);
                // ApplicationLog.WriteError(ApplicationLog.FormatException(y, "SimpleEncodeError"));
            }
            return str;
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static string SimpleDecode(string result)
        {
            return SimpleDecode(result, SECRET);

        }


        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="result"></param>
        /// <param name="constSecret"></param>
        /// <returns></returns>
        public static string SimpleDecode(string result, string constSecret)
        {
            string str = "";
            try
            {
                if (!result.Equals(""))
                {
                    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                    char[] cnB = constSecret.ToCharArray(0, constSecret.Length);
                    char[] rB = result.ToCharArray(0, result.Length);
                    int length = rB.Length / 3;
                    byte[] rs = new byte[length];
                    for (int i = 0; i < rB.Length; i += 3)
                    {
                        string s2 = result.Substring(i, 3);
                        int sum = Convert.ToInt32(s2);
                        int kn = (i / 3) % (constSecret.Length);
                        int k = (int)cnB[kn];
                        rs[i / 3] = Convert.ToByte(sum - k);
                    }
                    str = encoding.GetString(rs);
                }
            }
            catch //(Exception x)
            {
                //Exception y = new Exception(x.Message + "\n" + x.StackTrace);
                //ApplicationLog.WriteError(ApplicationLog.FormatException(y, "SimpleDecodeError"));
            }
            return str;
        }
        #endregion

    }

}
